part of common_lib_plugin;

extension DecimalExtension on Decimal {
  String truncateAsString([int fractionDigits = 2]) {
    return truncate(scale: fractionDigits).toString();
  }

  String formatNum({int position = 2}) {
    // ignore: prefer_typing_uninitialized_variables
    var num;
    if (this is double) {
      num = this;
    } else {
      num = double.parse(toString());
    }
    if ((num.toString().length - num.toString().lastIndexOf('.') - 1) < position) {
      return (num.toStringAsFixed(position).substring(0, num.toString().lastIndexOf('.') + position + 1).toString());
    } else {
      return (num.toString().substring(0, num.toString().lastIndexOf('.') + position + 1).toString());
    }
  }
}
